package com.tsinghuabigdata.edu.ddmath.adapter;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;

import com.tsinghuabigdata.edu.ddmath.R;
import com.tsinghuabigdata.edu.ddmath.bean.DayQuestions;
import com.tsinghuabigdata.edu.ddmath.bean.Question;
import com.tsinghuabigdata.edu.ddmath.module.onlineTutoring.adapter.DayQuestionsAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;



/**
 * Created by Administrator on 2017/3/3.
 */

public class SelectBaseAdapter extends BaseAdapter {

    private Context            mContext;
    private List<DayQuestions> mDatas;
    private List<HashMap<Integer, Boolean>> mMapList = new ArrayList<HashMap<Integer, Boolean>>();

    public SelectBaseAdapter(Context context, List<DayQuestions> list) {
        mDatas = list;
        mContext = context;
        for (int i = 0; i < list.size(); i++) {
            HashMap<Integer, Boolean> hashMap = new HashMap<Integer, Boolean>();
            mMapList.add(hashMap);
        }
    }


    @Override
    public int getCount() {
        if (mDatas != null) {
            return mDatas.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if (mDatas != null) {
            return mDatas.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        Log.i("sky", "getView position=" + position);
        ViewHolder holder = null;
        if (convertView == null) {
            // 没有复用
            convertView = View.inflate(mContext, R.layout.item_select_questions, null);
            holder = new ViewHolder();
            convertView.setTag(holder);
            // 给holder找view
            holder.cb = (CheckBox) convertView.findViewById(R.id.cb_date);
            holder.rv = (RecyclerView) convertView.findViewById(R.id.rv_day_questions);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        DayQuestions vo = mDatas.get(position);
        holder.cb.setText(vo.getDay());
        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
        layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        holder.rv.setLayoutManager(layoutManager);
        List<Question> teamplist = new ArrayList<Question>();
        teamplist.addAll(vo.getList());
//        if (holder.rv.getAdapter() == null) {
//            Log.i("sky", "getAdapter()==null");
//            DayQuestionsAdapter adapter = new DayQuestionsAdapter(mContext, teamplist);
//            adapter.setMap(mMapList.get(position));
//            holder.rv.setAdapter(adapter);
//        } else {
//            Log.i("sky", "getAdapter()!=null   list.size()=" + teamplist.size());
//            DayQuestionsAdapter adapter = (DayQuestionsAdapter) holder.rv.getAdapter();
//            adapter.setData(teamplist);
//            adapter.setMap(mMapList.get(position));
//            adapter.notifyDataSetChanged();
//        }
        holder.cb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CheckBox checkBox = (CheckBox) v;
                if (checkBox.isChecked()){
                    chooseAll(position,true);
                }else {
                    chooseAll(position,false);
                }
            }
        });

        return convertView;
    }

    private void chooseAll(int position, boolean all) {
        HashMap<Integer, Boolean> hashMap = mMapList.get(position);
        DayQuestions dayQuestions = mDatas.get(position);
        int size = dayQuestions.getList().size();
        for (int j = 0; j < size; j++) {
            hashMap.put(j, all);
        }
        notifyDataSetChanged();
    }

    public void cancel() {
        for (int i = 0; i < mMapList.size(); i++) {
            HashMap<Integer, Boolean> hashMap = mMapList.get(i);
            DayQuestions dayQuestions = mDatas.get(i);
            int size = dayQuestions.getList().size();
            for (int j = 0; j < size; j++) {
                hashMap.put(j, false);
            }
        }
        notifyDataSetChanged();
    }

    class ViewHolder {
        CheckBox     cb;
        RecyclerView rv;
    }

}

